﻿/*======================================
作者：洞庭夕照
创建：2017.8.18
网站：www.ninesky.cn
      mzwhj.cnblogs.com
代码：git.oschina.net/ninesky/Ninesky
版本：v1.0.0.0
======================================*/
using Ninesky.DBLibrary;
using Ninesky.Models;
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace Ninesky.IService
{
    /// <summary>
    /// 基础接口
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public interface IBaseService<T> where T : class
    {
        /// <summary>
        /// 数据上下文
        /// </summary>
         NsDbContext NsDbContext { get; set; }

        /// <summary>
        /// 添加实体
        /// </summary>
        /// <param name="entity">实体</param>
        /// <param name="isSave">是否立即保存[可选]</param>
        /// <returns>操作结果
        /// OperationResult.Succeed：操作是否完成，当isSave=false仅表示操作完成并未实际保存到数据库；
        /// OperationResult.Code：无返回值；
        /// OperationResult.Message：操作结果的文本提示；
        /// OperationResult.Data：无返回值。
        /// </returns>
        Task<OperationResult> AddAsync(T entity, bool isSave = true);


        /// <summary>
        /// 记录数
        /// </summary>
        /// <param name="predicate">查询条件</param>
        /// <returns>记录数</returns>
        Task<int> CountAsync(Expression<Func<T, bool>> predicate);

        /// <summary>
        /// 是否存在
        /// </summary>
        /// <param name="predicate">查询条件</param>
        /// <returns>是否存在</returns>
        Task<bool> ExistsAsync(Expression<Func<T, bool>> predicate);

        /// <summary>
        /// 查询单个实体
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns>实体</returns>
        Task<T> FindAsync(int id);

        /// <summary>
        /// 查询单个实体
        /// </summary>
        /// <param name="predicate">查询条件</param>
        /// <returns>实体</returns>
        Task<T> FindAsync(Expression<Func<T, bool>> predicate);

        /// <summary>
        /// 查询全部实体列表
        /// </summary>
        /// <returns>实体列表</returns>
        Task<IQueryable<T>> FindAllAsync();


        /// <summary>
        /// 查询实体列表
        /// </summary>
        /// <param name="number">显示数量[小于等于0-不启用]</param>
        /// <typeparam name="TKey">排序字段类型</typeparam>
        /// <param name="predicate">查询条件</param>
        /// <param name="keySelector">排序字段属性表达式</param>
        /// <param name="isAsc">是否正序</param>
        /// <returns>实体列表</returns>
        Task<IQueryable<T>> FindListAsync<TKey>(int number, Expression<Func<T, bool>> predicate, Expression<Func<T, TKey>> keySelector, bool isAsc);


        /// <summary>
        /// 查询实体分页数据
        /// </summary>
        /// <typeparam name="TKey">排序属性类型</typeparam>
        /// <param name="predicate">查询条件</param>
        /// <param name="keySelector">排序字段属性表达式</param>
        /// <param name="isAsc">是否正序</param>
        /// <param name="paging">分页数据</param>
        /// <returns>分页数据</returns>
        Task<Paging<T>> FindPagingAsync<TKey>(Expression<Func<T, bool>> predicate, Expression<Func<T, TKey>> keySelector, bool isAsc, Paging<T> paging);

        /// <summary>
        /// 删除实体
        /// </summary>
        /// <param name="entity">实体。【只需对实体主键赋值】</param>
        /// <param name="isSave">是否立即保存</param>
        /// <returns>操作结果
        /// OperationResult.Succeed：操作是否完成，当isSave=false仅表示操作完成并未实际从数据库中删除；
        /// OperationResult.Code：无返回值；
        /// OperationResult.Message：操作结果的文本提示；
        /// OperationResult.Data：无返回值。
        /// </returns>
        Task<OperationResult> RemoveAsync(T entity, bool isSave = true);

        /// <summary>
        ///  保存数据
        /// </summary>
        /// <returns>成功的记录数</returns>
        Task<int> SaveChangesAsync();

        /// <summary>
        /// 更新实体
        /// </summary>
        /// <param name="entity">实体</param>
        /// <param name="isSave">是否立即保存</param>
        /// <returns>操作结果
        /// OperationResult.Succeed：操作是否完成，当isSave=false仅表示操作完成并未实际保存到数据库；
        /// OperationResult.Code：无返回值；
        /// OperationResult.Message：操作结果的文本提示；
        /// OperationResult.Data：无返回值。
        /// </returns>
        Task<OperationResult> UpdateAsync(T entity, bool isSave = true);
    }
}
